import java.util.ArrayList;

class StateController
{
	private ArrayList<GameState> states;
	private int index;
	
	public StateController()
	{
		states = new ArrayList<GameState>();
		index = 0;
	}
	
	public void addState(GameState gs)
	{
		gs.init();
		states.add(gs);
	}
	
	public void removeState(GameState gs)
	{
		states.remove(gs);
	}
	
	public void nextState()
	{
		if(index == states.size()-1)
			return;
		
		states.get(index).leave();
		index++;
		states.get(index).enter();
	}
	
	public void previousState()
	{
		if(index == 0)
			return;
		
		states.get(index).leave();
		index--;
		states.get(index).enter();
	}
	
	public boolean jumpToState(int state)
	{
		if(state < 0 || state >= states.size())
			return false;
		
		states.get(index).leave();
		index = state;
		states.get(index).enter();
		
		return true;
	}
	
	public boolean jumpToState(String state)
	{
		for(int x = 0; x < states.size(); x++)
			if(states.get(x).getName().equals(state))
			{
				states.get(index).leave();
				index = x;
				states.get(index).enter();
				
				return true;
			}
		
		return false;
	}
	
	public GameState getCurrentState()
	{
		return states.get(index);
	}
}